package com.zhangc.blog.admin.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.zhangc.blog.admin.dto.SimpleBlogListVO;
import com.zhangc.blog.admin.entity.BlogCommentEntity;
import com.zhangc.blog.admin.entity.BlogInfoEntity;
import com.zhangc.blog.admin.entity.BlogTagRelationEntity;
import com.zhangc.blog.admin.mapper.BlogCommentMapper;
import com.zhangc.blog.admin.mapper.BlogInfoMapper;
import com.zhangc.blog.admin.mapper.BlogTagRelationMapper;
import com.zhangc.blog.base.enums.BlogStatusEnum;
import com.zhangc.blog.base.enums.DeleteStatusEnum;

@Service
public class BlogInfoServiceImpl extends ServiceImpl<BlogInfoMapper, BlogInfoEntity> {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Autowired
    private BlogTagRelationMapper blogTagRelationMapper;

    @Autowired
    private BlogCommentMapper blogCommentMapper;

    public List<SimpleBlogListVO> getNewBlog() {
        List<SimpleBlogListVO> simpleBlogListVOS = new ArrayList<>();
        Page<BlogInfoEntity> page = new Page<>(1, 5);
        blogInfoMapper.selectPage(page, new QueryWrapper<BlogInfoEntity>()
                .lambda()
                .eq(BlogInfoEntity::getBlogStatus, BlogStatusEnum.RELEASE.getStatus())
                .eq(BlogInfoEntity::getIsDeleted, DeleteStatusEnum.NO_DELETED.getStatus())
                .orderByDesc(BlogInfoEntity::getCreateTime));
        for (BlogInfoEntity blogInfo : page.getRecords()) {
            SimpleBlogListVO simpleBlogListVO = new SimpleBlogListVO();
            BeanUtils.copyProperties(blogInfo, simpleBlogListVO);
            simpleBlogListVOS.add(simpleBlogListVO);
        }
        return simpleBlogListVOS;
    }

    public List<SimpleBlogListVO> getHotBlog() {
        List<SimpleBlogListVO> simpleBlogListVOS = new ArrayList<>();
        Page<BlogInfoEntity> page = new Page<>(1, 5);
        blogInfoMapper.selectPage(page, new QueryWrapper<BlogInfoEntity>()
                .lambda()
                .eq(BlogInfoEntity::getBlogStatus, BlogStatusEnum.RELEASE.getStatus())
                .eq(BlogInfoEntity::getIsDeleted, DeleteStatusEnum.NO_DELETED.getStatus())
                .orderByDesc(BlogInfoEntity::getBlogViews));
        for (BlogInfoEntity blogInfo : page.getRecords()) {
            SimpleBlogListVO simpleBlogListVO = new SimpleBlogListVO();
            BeanUtils.copyProperties(blogInfo, simpleBlogListVO);
            simpleBlogListVOS.add(simpleBlogListVO);
        }
        return simpleBlogListVOS;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean clearBlogInfo(Long blogId) {
        if (SqlHelper.retBool(blogInfoMapper.deleteById(blogId))) {
            QueryWrapper<BlogTagRelationEntity> tagRelationWrapper = new QueryWrapper<>();
            tagRelationWrapper.lambda().eq(BlogTagRelationEntity::getBlogId, blogId);
            blogTagRelationMapper.delete(tagRelationWrapper);
            QueryWrapper<BlogCommentEntity> commentWrapper = new QueryWrapper<>();
            commentWrapper.lambda().eq(BlogCommentEntity::getBlogId, blogId);
            blogCommentMapper.delete(commentWrapper);
            return true;
        }
        return false;
    }
}
